<?php
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\AntNest\Pay;
use AuctionCore\Common\AjaxPage;

class QuestionOrderController extends BaseController
{
    private $questionOrderLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();

        $this->questionOrderLogic = D('QuestionOrder', 'Logic');

        $this->empId = $this->user_info['emp_id'];
    }

    public function index()
    {
        $this->display();
    }

    /**
     *　普通提问管理列表
     */
    public function questionOrderList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['order_id'] = I('get.order_id', '', 'trim');
        $params['pay_id'] = I('get.pay_id', '', 'trim');
        $params['pay_type'] = I('get.pay_type', '');
        $params['question_id'] = I('get.question_id/d', 0);
        $params['uid'] = I('get.uid/d', 0);
        $params['user_name'] = I('get.user_name', '', 'trim');
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $params['return_start_time'] = I('get.return_start_time', '');
        $params['return_end_time'] = I('get.return_end_time', '');
        $params['status'] = I('get.status/d', 0);
        $result = $this->questionOrderLogic->getQuestionOrderList($page, $size, $params);
        $list = !empty($result['list']) ? $result['list'] : [];
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('total', $result['total']);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 付费提问订单管理详情
     */
    public function questionOrderInfo()
    {
        $id = I('get.id/d', 0);
        if ($id < 1) {
            $this->redirect('QuestionOrder/index', '', 1, '请求出错了');
        }
        $detail = $this->questionOrderLogic->getQuestionOrderInfo($id);
        $this->assign('detail', $detail);
        $this->display();
    }

    public function questionOrderInfoJson()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $detail = $this->questionOrderLogic->getQuestionOrderInfo($id);
        echo json_encode($detail);
    }

    /**
     * 退款操作
     */
    public function questionOrderRefund()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $detail = $this->questionOrderLogic->getQuestionOrderInfo($id);
        if ($detail['status'] == 3) {
            $this->helpJsonResult('400', '该订单已退款');
        }
        $refundNoticeUrl = getCurHostUrl() . U('Notify/refund');
        $antPay = new Pay();
        $result = $antPay->refund($detail['pay_no'], $detail['pay_sn'], $refundNoticeUrl, '提问付费订单' . $detail['pay_no'] . '退款');
        if (!empty($result['refund_result'])) { //不需要跳出 example:lakala
            if ($result['refund_result']['status'] != 1) {
                $this->helpJsonResult('400', $result['refund_result']['msg']);
            }
            $order['id'] = $id;
            $order['emp_id'] = $this->empId;
            $this->questionOrderLogic->updateQuestionOrder($order);
            $this->helpJsonResult('200', '请求成功');
        } else { //需要跳出 example:alipay
            if (empty($result['refund_url'])) {
                $this->helpJsonResult('400', '退款请求失败');
            }
            $order['id'] = $id;
            $order['emp_id'] = $this->empId;
            $this->questionOrderLogic->updateQuestionOrder($order);
            $this->helpJsonResult('200', '请求成功', $result['refund_url']);
        }
    }
}